package at.tugraz.akm.forume;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import at.tugraz.akm.forume.extras.JSONUtils;
import at.tugraz.akm.forume.extras.MySharedPreferences;
import at.tugraz.akm.forume.extras.Utils;
import at.tugraz.akm.forume.webservice.Client;

public class Login extends Activity {

	Button login;
	Button create;
	EditText username;
	EditText password;
	TextView error;
	LinearLayout ll_buttons;
	ProgressBar loading;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		if (Utils.isLogged(getApplicationContext())) {
			startActivity(new Intent(getApplicationContext(), Main.class)
					.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		}

		login = (Button) findViewById(R.id.enter);
		create= (Button) findViewById(R.id.create_account);
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		error = (TextView) findViewById(R.id.error_text);
		ll_buttons = (LinearLayout) findViewById(R.id.buttons);
		loading = (ProgressBar) findViewById(R.id.loading);

		create.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(Login.this, NewUser.class)
				.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			}
		});
		
		login.setOnClickListener(click_login);
		
		
	}

	OnClickListener click_login = new OnClickListener() {

		@Override
		public void onClick(View v) {

			ll_buttons.setVisibility(View.GONE);
			username.setVisibility(View.GONE);
			password.setVisibility(View.GONE);
			loading.setVisibility(View.VISIBLE);
			error.setVisibility(View.GONE);

			new EnterTask(Login.this, username.getText().toString().trim(),
					password.getText().toString().trim(), ll_buttons, loading,
					username, password, error).execute();
		}
	};

	public class EnterTask extends AsyncTask<Void, Void, Void> {

		String username;
		String password;
		Context context;
		LinearLayout buttons;
		ProgressBar loading;
		EditText et_username;
		EditText et_password;
		TextView error_text;

		public EnterTask(Context context, String username, String password,
				LinearLayout buttons, ProgressBar loading,
				EditText et_username, EditText et_password, TextView error_text) {
			this.context = context;
			this.username = username;
			this.password = password;
			this.buttons = buttons;
			this.loading = loading;
			this.et_username = et_username;
			this.et_password = et_password;
			this.error_text = error_text;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		public void error() {
			error_text.setVisibility(View.VISIBLE);
			buttons.setVisibility(View.VISIBLE);
			et_username.setVisibility(View.VISIBLE);
			et_password.setVisibility(View.VISIBLE);
			loading.setVisibility(View.GONE);
		}

		@Override
		protected void onPostExecute(Void voids) {
			if (username.length() > 0 && password.length() > 0) {
				if (!Client.checkUser(username)) {
					error_text.setText(context.getText(R.string.error_no_user));
					error();
				} else if (Client.logIn(username, password)) {
					MySharedPreferences.setString(getApplicationContext(),
							MySharedPreferences.USERNAME, username);
					MySharedPreferences.setString(getApplicationContext(),
							MySharedPreferences.PASSWORD, password);
					String user_id = JSONUtils.getString(
							"http://nibble.li/forume/user.php?action=user_id&username="
									+ username, "user_id");
					MySharedPreferences.setString(getApplicationContext(),
							MySharedPreferences.USER_ID, user_id);
					startActivity(new Intent(context, Main.class)
							.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				} else {
					error_text
							.setText(context.getText(R.string.error_password));
					error();
				}
			} else {
				error_text.setText(context.getText(R.string.error_field_empty));
				error();
			}
		}
	}
}